/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class Filer {
    static char fileSeparate = System.getProperty("file.separator").charAt(0);

    public static String load(String packageName, String fileName, String pRootDir) {
        String dirName = packageName.replace('.', fileSeparate);
        return read(pRootDir + dirName +fileSeparate, fileName);
    }

    public static String read(File file) {
        try {
            if (!file.exists() || file.isDirectory()) {
                return null;
            }

            FileReader fileReader = new FileReader(file);
            BufferedReader in = new BufferedReader(fileReader);
            StringBuffer stringBuf = new StringBuffer();

            while (true) {
                int read = in.read();

                if (read == -1) {
                    break;
                }

                stringBuf.append(new Character((char) read));
            }

            fileReader.close();
            in.close();

            return stringBuf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    private static boolean isText(String name) {
        return name.endsWith("xml")
        ||name.endsWith("htm")
        ||name.endsWith("html")
            ||name.endsWith("txt")
            ||name.endsWith("xsp");
    }
    private static boolean isImage(String name) {
        return name.endsWith("jpg")
            ||name.endsWith("gif")
            ||name.endsWith("png")
            ||name.endsWith("img");
    }
    public static String read(String dir, String fileName) {
        if(isText(fileName))
        {
            File file = new File(dir  + fileName);
            return read(file);
        }else if(isImage(fileName)) {
            return "<a href=\""+fileName+"\" >" +
                        "<img src=\""+fileName+"\" style=\"width:400px;\"/></a>";
        }//isVideo, and all supported file
        return "unsupported file type "+fileName;
    }

    private static void save(String packageName, String fileName, String pRootDir, String pCode) {
        try {
            String dirName = (pRootDir + packageName).replace('.', fileSeparate);

            if (!(new File(dirName)).exists()) {
                (new File(dirName)).mkdirs();
            }

            dirName = dirName + fileSeparate + fileName;

            FileWriter aWriter = new FileWriter(dirName, false);
            aWriter.write(pCode);
            aWriter.flush();
            aWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void save(File file, String value) {
        try {
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter aWriter = new FileWriter(file, false);
            aWriter.write(value);
            aWriter.flush();
            aWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void save(String fileName, String value) {
        save(new File(fileName), value);
    }
}
